package br.com.rookgraph.heuristica;

import br.com.rookgraph.Edge;
import br.com.rookgraph.Graph;
import br.com.rookgraph.Swap;

public class NotEstanque implements Heuristica{ 

	public boolean isBest(Graph tree, Swap candidate, Swap best) {
		
		if (best == null || (tree.diffBranch(candidate) < tree.diffBranch(best))){
		//if (tree.diffBranch(candidate) < tree.diffBranch(best)){
			return true;

		}else if (tree.diffBranch(candidate) == tree.diffBranch(best)) { 

			if (tree.lambda(candidate) < tree.lambda(best)) {
				return true;

			}else if (tree.lambda(candidate) == tree.lambda(best)) { 

				if (tree.diffGama(candidate) < tree.diffGama(best)) {
					return true;
				}
			}
		}
		return false;
	}

	@Override
	public Edge selectForCut(Edge candidate, Edge edgeCut) {

			Edge edgeSelect = edgeCut;

			if (edgeSelect == null || candidate.beta < edgeSelect.beta) {
				edgeSelect = candidate;

			} else if (candidate.beta == edgeSelect.beta) {
				if (candidate.gama < edgeSelect.gama) {
					edgeSelect = candidate;

				}else if (candidate.gama == edgeSelect.gama){
					
					if( candidate.sigma > edgeSelect.sigma){
						edgeSelect = candidate;

					}else if( candidate.sigma == edgeSelect.sigma){

						if( candidate.sigmaLine > edgeSelect.sigmaLine){
							edgeSelect = candidate;
						}
					}
				}
			}

			return edgeSelect;
		}

	@Override
	public Edge selectForRep(Edge candidate, Edge edgeRep) {

		if (edgeRep == null || Math.abs(candidate.beta) < Math.abs(edgeRep.beta)) {
			edgeRep = candidate;
		}
		else if (candidate.beta == edgeRep.beta) {
			if (candidate.gama < edgeRep.gama) {
				edgeRep = candidate;

			}else if (candidate.gama == edgeRep.gama){

				if (candidate.sigma > edgeRep.sigma){
					edgeRep = candidate;
									
				}else if( candidate.sigma == edgeRep.sigma){

					if( candidate.sigmaLine < edgeRep.sigmaLine){
						edgeRep = candidate;
					}
				}
			}
		}
		return edgeRep;
	}

	@Override
	public boolean isVantajoso(Edge edgeCut, Edge edgeRep) {
		
		if(edgeRep.beta < edgeCut.beta){
		   return true;
		}else {
			if((edgeRep.beta == edgeCut.beta) &&
				(edgeRep.gama < edgeCut.gama)){
				return true;
				}			
		}
		return false;
	}
}
			
